/* This is a simple program that prints some text
 * on the Malta LCD display. It is started with the
 * OS and runs in the background.
 * It will be started after the main command shell,
 * to have that shell as a supervisor and to be able
 * to control and restart it.
 *
 * It uses the ecrire_LCD() and the retarder() API functions.
 */

#include "scroller.h"

void scroller(){

  //We get this pointer as an argument, 
  //it is going to be changed during the program execution
  char* input = "PORN-OS OR No OS! "; 

  
  //the part of the string that is foing to be printed fits in here
  char display_buffer[DISPLAY_LENGTH+1];
  display_buffer[DISPLAY_LENGTH] = '\0'; // \zero termination

  //measure the string length
  int len = strlen(input);

  int input_start_pos = 0; //the index in input that we start to read from
  int display_start_pos = 0; //the index in the display that we start to write to

  while(TRUE){

    //copy the interesting part of the string into the display buffer
    int i;
    for(i=0; i<DISPLAY_LENGTH; i++){
      
      //copy the correct part of the input string into the display buffer
      //this differs a bit depending if the string is longer or shorter than the display length

      if(len <= DISPLAY_LENGTH){ //We need Whitespaces sometimes

	if(i < len){           //check, where we need to fill a part of the display buffer with whitespaces     
	  display_buffer[(display_start_pos+i) % DISPLAY_LENGTH] = input[(i) % len];
	}else{
	  display_buffer[(display_start_pos+i) % DISPLAY_LENGTH] = WHITESPACE;
	}

      } else { //We don't need to fill with Whitespaces

	  display_buffer[i] = input[(input_start_pos+i) % len];

      }
      
    }

    //print and sleep
    ecrire_lcd(display_buffer);
    retarder(obtenir_pid(), 1700);

    input_start_pos = (input_start_pos + 1) % len;
    display_start_pos = (display_start_pos + 1) % DISPLAY_LENGTH;

  }

  //return exit_success;
  quitter(0);

}
